import { setCustomTitle } from '@/utils/unit'
import {
  getStatWorkbenchOverview,
  getStatStatByLevel,
  getStatStatByDay,
  getStatStatBySource,
  getStatTimeSpendRank,
  getStatStatByOrderType,
  getStatStatRegionStat,
  getStatStatByOfficeAndLevel,
} from '@/apis/statistics/index.js'

export const useStatistics = (params) => {
  const statisticsLoading = ref(false)

  const overviewData = ref({})
  const statByLevelData = ref({})
  const statByDayData = ref({})
  const statStatBySourceData = ref({})
  const statStatRegionStatData = ref({})
  const timeSpendRankData = ref([])
  const statStatBySourceTagsData = ref([])
  const statStatByOrderTagsData = ref([])
  const statStatByOfficeAndLevelData = ref([])
  const statStatByOrderData = ref({})

  // 获取统计总览数据
  async function getOverviewData(query) {
    const res = await getStatWorkbenchOverview(query)
    statisticsLoading.value = false
    if (res) {
      const {
        ret,
        msg
      } = res
      if (ret === 0) {
        console.log(res.data)
        overviewData.value = res.data
      }
    }
  }

  // 获取类别分析数据
  async function getStatByLevelData(query) {
    statByLevelData.value = {}
    const res = await getStatStatByLevel(query)
    statisticsLoading.value = false
    if (res) {
      const {
        ret,
        msg
      } = res
      if (ret === 0) {
        const datas = res.data?.map(item => {
          return {
            name: item.label,
            value: item.total,
            itemStyle: { color: item.label==='敏感信息'?'#2080f0':item.label==='一般敏感信息'?'#facc15':item.label==='重要敏感信息'?'#fb923c':'#fc4958'} 
          }
        })

        const totals = datas?.reduce(
          (accumulator, currentValue) => accumulator + currentValue.value,
          0
        )

        statByLevelData.value = {
          title: '工单总数',
          total:totals,
          seriesData: datas
        }
      }
    }
  }

  // 获取舆情走势分析数据
  async function getStatByDayData(query) {
    const res = await getStatStatByDay(query)
    statisticsLoading.value = false
    if (res) {
      const {
        ret,
        msg
      } = res
      if (ret === 0) {
        const xData = []
        const seriesData = []
        res.data?.map(item => {
          xData.push(item.label)
          seriesData.push(item.total)
          return item
        })

        statByDayData.value = {
          title: '舆情走势',
          xData,
          seriesData,
        }
      }
    }
  }


// 获取舆情来源数据
async function getStatStatBySourceData(query) {
  statStatBySourceData.value = {}
  const res = await getStatStatBySource(query)
  statStatBySourceTagsData.value = []
  statisticsLoading.value = false
  if (res) {
    const {
      ret,
      msg
    } = res
    if (ret === 0) {
      const dataArry = res.data
      const itemNum = dataArry.map(item=>item.total)
      const sourceTotal = itemNum.reduce((accumulator, current) => accumulator + current, 0);
     
      
      const datas = dataArry?.map(item => {
        const percent = ((item.total / sourceTotal) * 100).toFixed(0)
        const texts = `${item.label} -${percent} - ${item.total}`
        statStatBySourceTagsData.value.push({
          label:item.label,
          percent,
          value:item.total
        })
        return {
          name: item.label,
          value: item.total,
        }
      })
    
      statStatBySourceData.value = {
        title: '工单总数',
        total: sourceTotal,
        seriesData: datas
      }
    }
  }
}


// 处室统计 直属单位统计
async function getStatStatByOfficeAndLevelData(query) {
  statStatByOfficeAndLevelData.value = []
  const res = await getStatStatByOfficeAndLevel(query)
  statisticsLoading.value = false
  if (res) {
    const {
      ret,
      msg
    } = res
    if (ret === 0) {
      const datas = res.data
      statStatByOfficeAndLevelData.value = datas
    }
  }
}

// 分类获取舆情数量数据
async function getStatStatRegionStatData(query) {
  statStatRegionStatData.value = []
  const res = await getStatStatRegionStat(query)
  statisticsLoading.value = false
  if (res) {
    const {
      ret,
      msg
    } = res
    if (ret === 0) {
      const datas = res.data
      statStatRegionStatData.value = datas?.map(item=>{
        item.label = setCustomTitle(item.label,8)
        return item
      })
    }
  }
}

// 获取处理时长统计
async function getTimeSpendRankData(query) {
  const res = await getStatTimeSpendRank(query)
  statisticsLoading.value = false
  if (res) {
    const {
      ret,
      msg
    } = res
    if (ret === 0) {
      const datas = res.data
      timeSpendRankData.value = datas
    }
  }
}

// 获取类别统计
async function getStatStatByOrderData(query) {
  statStatBySourceData.value.seriesData = []
  statStatBySourceTagsData.value = []
  const res = await getStatStatByOrderType(query)
  statisticsLoading.value = false
 
  if (res) {
    const {
      ret,
      msg
    } = res
    if (ret === 0) {
      const dataArry = res.data
      const itemNum = dataArry.map(item=>item.total)
      const total = itemNum.reduce((accumulator, current) => accumulator + current, 0);
     
      const datas = dataArry?.map(item => {
        const percent = ((item.total / total) * 100).toFixed(0)
        statStatBySourceTagsData.value.push({
          label:item.label,
          percent,
          value:item.total
        })
        return {
          name: item.label,
          value: item.total,
        }
      })
      statStatBySourceData.value = {
        title: '工单总数',
        total,
        seriesData: datas
      }
    }
  }
}
  return {
    statisticsLoading,
    overviewData,
    getOverviewData,
    statByLevelData,
    getStatByLevelData,
    getStatByDayData,
    statByDayData,
    getStatStatBySourceData,
    statStatBySourceData,
    statStatBySourceTagsData,
    statStatByOrderTagsData,
    getStatStatRegionStatData,
    statStatRegionStatData,
    getTimeSpendRankData,
    timeSpendRankData,
    getStatStatByOrderData,
    statStatByOrderData,
    getStatStatByOfficeAndLevelData,
    statStatByOfficeAndLevelData
  }
}